$(function () {
    init_edit_user();
    init_grade();
});

/**初始化数据**/
function init_edit_user() {

    /**初始化验证信息**/
    var $userAddForm = $("#user-edit-form");
    //验证规则初始化
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw color-red'></i> ";
    var validator = $userAddForm.validate({
        rules: {
            nickname:{required: true},
            roles: {required: true}
        },
        messages: {
            nickname:icon + "请填写用户昵称",
            roles: icon + "请选择用户角色"
        }
    });
    /**初始化角色**/
    var $rolesSelect = $userAddForm.find("select[name='rolesSelect']");
    var $roles = $userAddForm.find("input[name='roles']");
    $.post(ctx + "role/list", {}, function (r) {
        let data = r.rows;
        let option = "";
        for (let i = 0; i < data.length; i++) {
            option += "<option value='" + data[i].roleId + "'>" + data[i].roleName + "</option>"
        }
        $rolesSelect.html("").append(option);
        let options = {
            selectAllText: '所有角色',
            allSelected: '所有角色',
            width: '100%',
            onClose: function () {
                $roles.val($rolesSelect.val());
                validator.element("input[name='roles']");
            }
        };
        /**初始化部门树**/
        $.post(ctx + "dept/tree", {}, function (r) {
            if (r.code === 0) {
                const data = r.msg;
                $('#deptTreeEdit').jstree({
                    "core": {
                        'data': data.children,
                        'multiple': false ,// 取消多选
                    },
                    "checkbox": {
                        "three_state": false // 取消选择父节点后选中所有子节点
                    },
                    "plugins": ["wholerow", "checkbox"]
                });
                init_edit_data();
            } else {
                $JuXue.n_danger(r.msg);
            }
        });
        $rolesSelect.multipleSelect(options);
    });

}
/**初始化数据**/
function init_edit_data(){
    /**获取用户信息**/
    var $userAddForm = $("#user-edit-form");
    var $rolesSelect = $userAddForm.find("select[name='rolesSelect']");
    var $roles = $userAddForm.find("input[name='roles']");

    var selected = $("#user-data-table").bootstrapTable('getSelections');
    var selected_length = selected.length;
    if (!selected_length) {$JuXue.n_warning('请勾选需要修改的用户！');return;}
    if (selected_length > 1) {$JuXue.n_warning('一次只能修改一个用户！');return;}

    var userId = selected[0].userId;
    $.post(ctx + "user/getUser", {"userId": userId}, function (r) {
        if (r.code === 0) {
            var user = r.msg;
            $("#username").val(user.username);
            $("#nickname").val(user.nickname);
            $('#userId').val(user.userId);
            $('#email').val(user.email);
            $("#mobile").val(user.mobile);
            $("#byCode").val(user.byCode);
            $("#deptId").val(user.deptId);
            $("#grade").val(user.grade);
            $("#extensionNumber").val(user.extensionNumber);
            let roleArr = [];
            for (let i = 0; i < user.roleIds.length; i++) {
                roleArr.push(user.roleIds[i]);
            }
            $rolesSelect.multipleSelect('setSelects', roleArr);
            $roles.val($rolesSelect.val());

            $("#deptTreeEdit").jstree().select_node(user.deptId,true,true);
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
}

/**更新用户**/
function update_Edit_User() {
    var treeNode = $('#deptTreeEdit').jstree().get_selected(false);
    if (treeNode == ""){$JuXue.n_danger("请选择部门");return }
    $("#deptId").val(treeNode[0]);
    var userifo = $("#user-edit-form").serialize();
    $.post(ctx + "user/update",  userifo, function (r) {
        if (r.code === 0) {
            $("#user_info").modal("hide");
            $JuXue.n_success(r.msg);
            $JuXue.refreshTable("user-data-table");
        } else $JuXue.n_danger(r.msg);
    });
}


function init_grade() {
    $.getJSON(ctx+"grade/list",{},function (r) {
        if(r.code===0){
            var data = r.data;
            $.each(data,function (index,val) {
                var id = val.gId;
                var name = val.gName;
                $("#grade").append('<option class="form-control" value="'+id+'">'+name+'</option>')
            })
        }
    })
}